﻿using Assets.Script.HelpSystem.AtkSystem.Base;
using Assets.Script.Human.Player;
using Assets.Script.Tools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEngine;

namespace Assets.Script.HelpSystem.AtkSystem
{
    public partial class AtkManager
    {
        public string WeaponName { get; } = "Gun";
        public int SlotNum { get; set; } = 3;

        private PlayerBody playerBody;
        private AtkWay[] atkWays;
        private int nowIndex = 0;

        /// <summary>
        /// 子弹效果字典
        /// <para>Type,等级</para>
        /// </summary>
        internal Dictionary<Type, int> BulletEffectLevel = new Dictionary<Type, int>();

        /// <summary>
        /// 只给子类用的构造函数
        /// </summary>
        /// <param name="playerBody">PlayerBody(玩家本体)</param>
        /// <param name="attackWay">攻击方式</param>
        public AtkManager(PlayerBody playerBody)
        { 
            this.playerBody = playerBody;
            atkWays = GetAktWay(playerBody.AtkInfos);
        }


        #region 供给外界调用的攻击相关指令与动作
        /// <summary>
        /// 设置子弹效果
        /// </summary>
        /// <param name="beType">子弹效果类Type</param>
        /// <param name="level">效果等级,<=0则为取消效果</param>
        public void SetBulletEffect(Type beType, int level)
        {
            if (BulletEffectLevel.ContainsKey(beType))
            {
                if (level <= 0)
                {
                    BulletEffectLevel.Remove(beType);
                }
                else
                {
                    BulletEffectLevel[beType] = level;
                }
            }
        }

        /// <summary>
        /// 开始执行指定的攻击动作
        /// </summary>
        /// <param name="atkWayEnmu"></param>
        public void AttackStart(AtkWayEnmu atkWayEnmu)
        {
            atkWays[nowIndex].AttackInterrupt();
            nowIndex = (int)atkWayEnmu;
            atkWays[nowIndex].AttackStart();
        }

        /// <summary>
        /// 用于网络端
        /// </summary>
        /// <param name="atkWay">枚举对应</param>
        public void AttackStart(int atkIndex)
        {
            atkWays[nowIndex].AttackInterrupt();
            nowIndex = atkIndex;
            atkWays[nowIndex].AttackStart();
        }
        
        /// <summary>
        /// 攻击结束事件
        /// </summary>
        public void AttackEnd(AtkWayEnmu atkWayEnmu)
        {
            nowIndex = (int)atkWayEnmu;
            atkWays[nowIndex].AttackEnd();
        }
        
        public void AttackEnd()
        {
            AttackEnd((AtkWayEnmu)nowIndex);
        }
        
        /// <summary>
        /// 打断攻击事件
        /// </summary>
        public virtual void AttackInterrupt()
        {
            atkWays[nowIndex].AttackInterrupt();
        }

        /// <summary>
        /// 每帧动作(即使不在攻击也会调用),调用每个atkWay
        /// </summary>
        public void Update()
        {
            foreach (var item in atkWays)
            {
                item.Update();
            }
        }
        #endregion


        private AtkWay[] GetAktWay(AtkInfo[] atkInfos)
        {
            AtkWay[] atkWays= new AtkWay[atkInfos.Length];
            Assembly AsmDevice = Assembly.GetExecutingAssembly();//加载命名空间    

            for (int i = 0; i < atkInfos.Length; i++)
            {
                Type classType = AsmDevice.GetType("Assets.Script.HelpSystem.AtkSystem.AtkWays." + atkInfos[i].ClassName);//获取命名空间下的类
                object classObj = Activator.CreateInstance(classType, new object[] { playerBody, atkInfos[i], BulletEffectLevel,i });//实例化反射回来的指定类
                atkWays[i] = (AtkWay)classObj;
            }
            return atkWays;
        } 
    }
}


